package cn.tsinghua.comsmacitccntsinghua.httpservice;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.tsinghua.comsmacitccntsinghua.tsinterface.IResponse;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.IStringResponse;
import lombok.Data;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

@Data
public class TsHttp {
    private  static TsHttp tsHttp;
    private OkHttpClient okHttpClient;

    public static TsHttp getInstance(){
         if(null==tsHttp){
             synchronized (TsHttp.class){
                 if(null==tsHttp){
                     tsHttp=new TsHttp();
                      OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(5, TimeUnit.SECONDS)
                            .readTimeout(5, TimeUnit.SECONDS)
                            .build();
                     tsHttp.setOkHttpClient(okHttpClient);
                 }
             }
         }
         return  tsHttp;
}

    public void get(String url,final IStringResponse myResponse){
        try{
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8;");
            final Request request = new Request.Builder()
                    .url(url)
                    .build();
           // url= URLEncoder.encode(url,"utf-8");
            Call call = this.okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    myResponse.onFail(0);
                }

                @Override
                public void onResponse(Call call, Response response) {
                    if (response.isSuccessful()) {
                        String result = null;
                        try {
                            result = response.body().string();
                        } catch (IOException e) {

                        }
                        myResponse.onSuccess(result);
                    }else{
                        myResponse.onFail(response.code());
                    }
                }
            });
        }catch (Exception e) {
            myResponse.onFail(-1);
            e.printStackTrace();
        }
    }

    public void get(String url,final IResponse myResponse){
        try{
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8;");
            final Request request = new Request.Builder()
                    .url(url)
                    .build();
            // url= URLEncoder.encode(url,"utf-8");
            Call call = this.okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    myResponse.onFail(-2);
                }

                @Override
                public void onResponse(Call call, Response response) {
                    try {
                        myResponse.onSuccess(response);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void post(final String url, final String body, final IStringResponse myResponse){
            try{
                // url= URLEncoder.encode(url,"utf-8");
                RequestBody req = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                        , body);
                Request request = new Request.Builder()
                        .url(url)
                        .post(req)
                        .build();
                Call call =okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        myResponse.onFail(0);
                        Log.d("RequestFF","请求失败" + url);
                    }
                    @Override
                    public void onResponse(Call call, Response response) {
                        if (response.isSuccessful()) {
                            String result = null;
                            try {
                                result = response.body().string();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            myResponse.onSuccess(result);
                        }else{
                            myResponse.onFail(response.code());
                        }
                    }
                });
            }catch (Exception e){
            myResponse.onFail(-1);
            e.printStackTrace();
        }
    }
}
